package com.zhuozhou.jdyc.main.meinfo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.zhuozhou.jdyc.R;
import com.zhuozhou.jdyc.api.UserAccountHelper;
import com.zhuozhou.jdyc.base.BaseActivity;
import com.zhuozhou.jdyc.databinding.ActivitySpoorAndCollectBinding;
import com.zhuozhou.jdyc.main.goods.activity.GoodsDetailActivity;
import com.zhuozhou.jdyc.main.main.adapter.IndexTabMenuAdapter;
import com.zhuozhou.jdyc.main.main.bean.TabItemBean;
import com.zhuozhou.jdyc.main.meinfo.adapter.SCShopAdapter;
import com.zhuozhou.jdyc.main.meinfo.bean.CollectBean;
import com.zhuozhou.jdyc.main.meinfo.viewmodel.MeInfoViewModel;
import com.zhuozhou.jdyc.wight.recyclerview.FullyGridLayoutManager;
import com.zhuozhou.jdyc.wight.recyclerview.MyLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Create by Once_ChuXy on 2021/9/28
 * Describe:足迹、收藏   关注Concern
 */
public class SpoorAndCollectActivity extends BaseActivity<MeInfoViewModel, ActivitySpoorAndCollectBinding> {

    private IndexTabMenuAdapter indexTabMenuAdapter;
    private SCShopAdapter scShopAdapter;
    private List<TabItemBean> tabMenuList = new ArrayList<>();
    private int Type = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_spoor_and_collect;
    }

    @Override
    public String setTitleBar() {
        return "商品收藏";
    }

    @Override
    public void initView() {
        tabMenuList = new ArrayList<>();
        tabMenuList.add(new TabItemBean(0, "聚点商品", Type == 1));
        tabMenuList.add(new TabItemBean(1, "积分商品", Type == 2));
        tabMenuList.add(new TabItemBean(2, "易货商品", Type == 3));
        mViewModel.BasicsMyCollectionMutableLiveData.observe(this, v -> {
            setScShopAdapter(v);
        });
        mViewModel.BasicsFootprintMutableLiveData.observe(this, v -> {
            setScShopAdapter(v.getArray().getList());
        });
    }

    public void setScShopAdapter(List<CollectBean> list) {
        indexTabMenuAdapter = new IndexTabMenuAdapter(this);
        indexTabMenuAdapter.setList(tabMenuList);
        indexTabMenuAdapter.setTextColor(R.color.grey_700);
        indexTabMenuAdapter.setViewWidth(binding.mRecyclerViewTopMenu.getWidth() / 3);
        indexTabMenuAdapter.setOnTabMenuItemClickListener((view, position) -> {
            Type = position + 1;
            if (baseBundle.getInt("type", 0) == 1)
                mViewModel.getBasicsMyCollection(Type, UserAccountHelper.getUserID() + "");
            else
                mViewModel.getBasicsFootprint(Type, UserAccountHelper.getUserID() + "");
        });
        binding.mRecyclerViewTopMenu.setAdapter(indexTabMenuAdapter);
        binding.mRecyclerViewTopMenu.setLayoutManager(new FullyGridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        if (list == null || list.size() == 0) {
            binding.tvNoOrder.setVisibility(View.VISIBLE);
            binding.mRecyclerViewOrder.setVisibility(View.GONE);
            return;
        } else {
            binding.tvNoOrder.setVisibility(View.GONE);
            binding.mRecyclerViewOrder.setVisibility(View.VISIBLE);
            scShopAdapter = new SCShopAdapter(this);
            scShopAdapter.setType(Type);
            scShopAdapter.setList(list);
            scShopAdapter.setOnItemButtonClickListener(position -> {
                Intent intent = new Intent(this, GoodsDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", scShopAdapter.getList().get(position).getCommodity_id() + "");
                bundle.putInt("type", Type);
                intent.putExtras(bundle);
                startActivity(intent);
            });
            binding.mRecyclerViewOrder.setAdapter(scShopAdapter);
            binding.mRecyclerViewOrder.setLayoutManager(new MyLayoutManager(this));
        }
    }

    @Override
    public void initData(Bundle bundle) {
        if (baseBundle.getInt("type", 0) == 1) {
            toolbarConfig.setTitle("商品收藏");
            mViewModel.getBasicsMyCollection(Type, UserAccountHelper.getUserID() + "");
        } else {
            toolbarConfig.setTitle("我的足迹");
            mViewModel.getBasicsFootprint(Type, UserAccountHelper.getUserID() + "");
        }

    }
}
